<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增单位</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '查看',
              icon: 'clarity:eye-show-solid',
              title: '查看用户详情',
            },
            {
              icon: 'clarity:note-edit-line',
              title: '编辑用户资料',
              onClick: handUnitEditDrawer.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              title: '删除此账号',
              popConfirm: {
                title: '是否确认删除',
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <PracticeDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>

<script>
  import { defineComponent } from 'vue';
  import { BasicTable, TableAction, useTable } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { columns, searchFormSchema } from './practice.data';
  import PracticeDrawer from './PracticeDrawer.vue';
  import { getListPage } from '/@/api/cuisine/unit';
  import { useDrawer } from '/@/components/Drawer';

  export default defineComponent({
    name: 'Index',
    components: { BasicTable, PageWrapper, TableAction, PracticeDrawer },
    setup() {
      const [registerDrawer, { openDrawer, setDrawerProps }] = useDrawer();

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handUnitEditDrawer(record) {
        openDrawer(true, {
          isUpdate: true,
          record,
        });
      }

      function handleSuccess() {
        reload();
      }

      const [registerTable, { reload, updateTableDataRecord }] = useTable({
        title: '单位列表',
        rowKey: 'id',
        api: getListPage,
        columns,
        dataSource: [],
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      return { registerTable, registerDrawer, handleCreate, handUnitEditDrawer, handleSuccess };
    },
  });
</script>

<style scoped></style>
